package cn.chaos.tcp21;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import cn.chaos.bio01.Frame;
import cn.chaos.bio01.LengthFramer;

public class Tcp21Client {

    public static void main(String[] args) {

        String server = "localhost";
        int port = 11111;

        List<String> infos = Arrays.asList("1、项目中使用的数据库使用 master-slave 配置，备库与主库实现实时同步。",
                "2、每周对数据库执行一次全量备份。每天对数据库执行一次增量备份。", "3、数据库的备份保存在 AWS S3 上面。");

        for (String info : infos) {
            try (Socket socket = new Socket(server, port);) {

                //socket.setSoTimeout(60);
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                Frame frame = null;
                // frame = new DelimFramer(in);
                frame = new LengthFramer(in);

                frame.frameMsg(info.getBytes(StandardCharsets.UTF_8), out);

                String rinfo = new String(frame.nextMsg(), StandardCharsets.UTF_8);
                System.out.println("info >> " + rinfo);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("--end--");

    }
}
